package com.gaoyi.crm.control.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.gaoyi.crm.R;
import com.gaoyi.crm.databinding.DialogConfirmBinding;

public class ConfirmDialog extends BaseFragmentDialog<DialogConfirmBinding> {

    private static final String TITLE = "title";
    private final IDoneListener mListener;

    public static ConfirmDialog newInstance(Context context, int title, IDoneListener listener) {
        return newInstance(context, context.getString(title), listener);
    }

    public static ConfirmDialog newInstance(Context context, String title, IDoneListener listener) {
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        ConfirmDialog dialog = new ConfirmDialog(context, listener);
        dialog.setArguments(args);
        return dialog;
    }

    public ConfirmDialog(Context context, IDoneListener listener) {
        super(context);
        this.mListener = listener;
    }

    public void setTitle(String title){
        mBinding.tvTitle.setText(title);
    }

    @Override
    protected void initView() {
        Bundle bundle = getArguments();
        if (bundle == null) return;
        mBinding.tvTitle.setText(bundle.getString(TITLE));
        MyOnClickListener onClickListener = new MyOnClickListener();
        mBinding.btnCancel.setOnClickListener(onClickListener);
        mBinding.btnConfirm.setOnClickListener(onClickListener);
    }

    private class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.btnConfirm) {
                if (mListener != null) {
                    mListener.iDoneListener();
                }
            }
            hide();
        }
    }

    @Override
    protected DialogConfirmBinding getViewBinding() {
        return DialogConfirmBinding.inflate(getLayoutInflater());
    }

    @Override
    protected String getShowTag() {
        return CENTER;
    }

    public interface IDoneListener {
        void iDoneListener();
    }
}
